<%@ page contentType="text/html; charset=utf-8" %>
<%@ include file="/WEB-INF/jsp/includeTop.jsp" %>
<%@ include file="/WEB-INF/jsp/includeTaglib.jsp" %>

<form id="newFunctionTemplateForm">
<table width="100%" border="0" cellpadding="0" cellspacing="5">
  <tr>
    <td valign="top">
      <table class="dbborder" width="100%" border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td valign="bottom" class="blueTableBg">&nbsp;函数模板管理&nbsp;-&nbsp;新增模板</td>
        </tr>
        <tr>
          <td>
            <table width="100%" border="0" cellpadding="0" cellspacing="5">
              <tr>
                <td>
                  <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                      <td class="dashTopLeft"><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashTop" width=100%><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashTopRight"><img src="images/spacer.gif" width="7" height="7"></td>
                    </tr>
                    <tr>
                      <td class="dashLeft"><img src="images/spacer.gif" width="7" height="7"></td>
                      <td align="center">
                        <table width="100%" border="0" cellpadding="0" cellspacing="3">
                          <tr>
                            <td width="114" height="20" class="globalText">函数名称</td>
                            <td height="20" class="globalText">
                            	<input id="functionName" type="text" class="txtbox" size="20" name="functionName" value=""/>
                            </td>
                          </tr>
                          <tr>
                            <td width="114" height="20" class="globalText">函数模板描述</td>
                            <td height="20" class="globalText">
                            	<input id="description" type="text" class="txtbox" size="20" name="description" value="" />
                            </td>
                          </tr>
                          <tr>
                            <td width="114" height="20" class="globalText">函数类型</td>
                            <td height="20" class="globalText">
                            	<select name="functionTypeSlt">
                            	  <option value="0">数据函数</option>
                            	  <option value="1">系统函数</option>
                            	</select>
                            </td>
                          </tr>
                          <tr id="sysFunctionTR">
                            <td width="114" height="20" class="globalText">函数内容</td>
                            <td height="20" class="globalText">
                            	<select name="sysFunctionSlt">
                            	  <option value="0">日期</option>
                            	</select>
                            </td>
                          </tr>
                          <tr id="relationTableTR">
                            <td width="114" height="20" class="globalText">对应表</td>
                            <td height="20" class="globalText">
                            	<select name="relationTableSlt">
                            	  <option value="dorder">运单表</option>
                            	  <option value="bdepartment">组织结构表</option>
                            	  <option value="dclient">客户表</option>
                            	</select>
                            </td>
                          </tr>
                          <tr id="relationFieldTR">
                            <td width="114" height="20" class="globalText">对应字段</td>
                            <td height="20" class="globalText">
                            	<select name="relationFieldSlt">
                            	  <option value="orderid">运单编号</option>
                            	  <option value="cargono">货号</option>
                            	  <option value="cargoname">货物名称</option>
                            	  <option value="cargocount">件数</option>
                            	  <option value="clientid">客户编号</option>
                            	  <option value="shipper">发货人</option>
                            	  <option value="shipperphone">发货人电话</option>
                            	  <option value="shipperMP">发货人手机</option>
                            	  <option value="signaturer">收货人</option>
                            	  <option value="signaturephone">收货人电话</option>
                            	  <option value="signatureMP">收货人手机</option>
                            	  <option value="totalfee">总运费</option>
                            	  <option value="sendfee">已付费</option>
                            	  <option value="arrivefee">提付费</option>
                            	  <option value="monthfee">月结费</option>
                            	  <option value="gatherfee">代收款</option>
                            	  <option value="loaddeptid">起运地公司编号</option>
                            	  <option value="unloadeptid">目的地公司编号</option>
                            	  <option value="sendcargotype3">送货方式(1-自提，2-送货)</option>
                            	</select>
                            </td>
                          </tr>
                          <tr id="associatedFieldTR">
                            <td width="114" height="20" class="globalText">关联字段</td>
                            <td height="20" class="globalText">
                            	<select name="associatedFieldSlt">
                            	  <option value="sendaddress">收货地址</option>
                            	</select>
                            </td>
                          </tr>
                          <tr>
                            <td align="center" colspan="2">
                              <table border="0" cellpadding="0" cellspacing="2">
                                <tr>
                                  <td><input type="button" name="saveBtn" class="btnStyle" value="  保存  "></td>
                                  <td><input type="button" name="cancelBtn" class="btnStyle" value="  取消  "></td>
                                </tr>
                              </table>
                            </td>
                          </tr>
                        </table>
                      </td>
                      <td class="dashRight"><img src="images/spacer.gif" width="7" height="7"></td>
                    </tr>
                    <tr>
                      <td class="dashBottomLeft"><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashBottom" width=100%><img src="images/spacer.gif" width="7" height="7"></td>
                      <td class="dashBottomRight"><img src="images/spacer.gif" width="7" height="7"></td>
                    </tr>
                  </table>
                </td>
              </tr>
            </table>
          </td>
        </tr>
      </table>
    </td>
  </tr>
</table>
</form>

<script type="text/javascript">
	$(document).ready(function() {
    var newFunctionTemplateForm = $("form[id=newFunctionTemplateForm]");
    var saveBtn = newFunctionTemplateForm.find(":button[name=saveBtn]");
    var cancelBtn = newFunctionTemplateForm.find(":button[name=cancelBtn]");

    saveBtn.click(function() {
    	document.getElementById("newFunctionTemplateForm").action="<c:url value='newFunctionTemplate.html' />";
    	document.getElementById("newFunctionTemplateForm").method="post";
    	
		  var functionName = document.getElementById("functionName").value.trim();
			if (validateEmpty(functionName)) {
				alert("函数名称 不能为空");
				return false;
			}

	   var description = document.getElementById("description").value.trim();
		 if (validateEmpty(description)) {
				alert("函数模板描述 不能为空");
				return false;
		 }
		 
    	newFunctionTemplateForm.submit();
    });

    cancelBtn.click(function() {
    	document.getElementById("newFunctionTemplateForm").action = "<c:url value='functionTemplateMain.html' />";
    	document.getElementById("newFunctionTemplateForm").method="get";
    	newFunctionTemplateForm.submit();
    });
    
		//数据函数
    var functionTypeSlt = newFunctionTemplateForm.find("select[name=functionTypeSlt]");
    var relationTableSlt = newFunctionTemplateForm.find("select[name=relationTableSlt]");
    var relationFieldSlt = newFunctionTemplateForm.find("select[name=relationFieldSlt]");
 	  var associatedFieldSlt = newFunctionTemplateForm.find("select[name=associatedFieldSlt]");

 	  
    var relationTableTR = newFunctionTemplateForm.find("#relationTableTR");
    var relationFieldTR = newFunctionTemplateForm.find("#relationFieldTR");
 	  var	associatedFieldTR = newFunctionTemplateForm.find("#associatedFieldTR");
 	  
 	  var	sysFunctionTR = newFunctionTemplateForm.find("#sysFunctionTR");
 	 	sysFunctionTR.hide();

    functionTypeSlt.change(function() {
      relationTableSlt = newFunctionTemplateForm.find("select[name=relationTableSlt]");
      relationFieldSlt = newFunctionTemplateForm.find("select[name=relationFieldSlt]");
 	  	associatedFieldSlt = newFunctionTemplateForm.find("select[name=associatedFieldSlt]");
      
    	var currentSltValue = $(this).val();
    	if (currentSltValue == "0") {//数据函数
    		relationTableTR.show();
    		relationFieldTR.show();
    		associatedFieldTR.show();

      	sysFunctionTR.hide();
      } else {//系统函数
    	  relationTableTR.hide();
    	  relationFieldTR.hide();
    	  associatedFieldTR.hide();

    	  sysFunctionTR.show();
      }
    });

    registerRelationFieldSltOrReloadAndReRegister();

    relationTableSlt.change(function() {
    	var currentSltValue = $(this).val();
    	
    	if (currentSltValue == "bdepartment" || currentSltValue == "dclient") {
	    	associatedFieldSlt = newFunctionTemplateForm.find("select[name=associatedFieldSlt]");
      	$.get("<c:url value='associatedFieldEmptySelect.html' />", 
      	  function(data){
      			associatedFieldSlt.replaceWith($(data).html());	
        });
      }
        
    	if (currentSltValue == "dorder") {
      	$.get("<c:url value='relationFieldOrderSelect.html' />", 
      	  function(data){
        		relationFieldSlt.replaceWith($(data).html());
          			
        		registerRelationFieldSltOrReloadAndReRegister();

						//关联字段
		  			$.get("<c:url value='associatedFieldOrderSelect.html' />", 
			    	  function(data){
		  	       	associatedFieldSlt = newFunctionTemplateForm.find("select[name=associatedFieldSlt]");
			     			associatedFieldSlt.replaceWith($(data).html());
			      	});
        });
      } else if (currentSltValue == "bdepartment") {
        	$.get("<c:url value='relationFieldDepartmentSelect.html' />", 
 	      	  function(data){
        			relationFieldSlt.replaceWith($(data).html());
          			
        			registerRelationFieldSltOrReloadAndReRegister(); 		      			
 	        });
      } else if (currentSltValue == "dclient") {
      	$.get("<c:url value='relationFieldClientSelect.html' />", 
      	  function(data){
      			relationFieldSlt.replaceWith($(data).html());
      			
      			registerRelationFieldSltOrReloadAndReRegister(); 			
        });
      }

    });
    
    function registerRelationFieldSltOrReloadAndReRegister() {
  	  	relationFieldSlt = newFunctionTemplateForm.find("select[name=relationFieldSlt]");//Reload Slt
       	relationFieldSlt.change(function() {
	       	var currentSltValue = $(this).val();
	       	if (currentSltValue == "clientid") {//客户编号 -> 客户表
	       	  associatedFieldSlt = newFunctionTemplateForm.find("select[name=associatedFieldSlt]");

	   	     	$.get("<c:url value='associatedFieldClientSelect.html' />", 
	   	    	  function(data){
	   	     			associatedFieldSlt.replaceWith($(data).html());
	   	      	});
	         } else if (currentSltValue == "loaddeptid" || currentSltValue == "unloadeptid") {//起运地公司编号 or 目的地公司编号 -> 组织结构表
		       	  associatedFieldSlt = newFunctionTemplateForm.find("select[name=associatedFieldSlt]");

	         		if (currentSltValue == "loaddeptid") {
		   	     		$.get("<c:url value='associatedFieldLoadDepartmentSelect.html' />", 
	  	    	    	  function(data){
	  	    	     			associatedFieldSlt.replaceWith($(data).html());
  	    	      	});
	         		} else {
		   	     		$.get("<c:url value='associatedFieldUnloadDepartmentSelect.html' />", 
	   	    	    	  function(data){
	   	    	     			associatedFieldSlt.replaceWith($(data).html());
   	    	      	});
		         	}
         } else {
	       	  associatedFieldSlt = newFunctionTemplateForm.find("select[name=associatedFieldSlt]");

	 	     		$.get("<c:url value='associatedFieldEmptySelect.html' />", 
	  	    	  function(data){
	  	     			associatedFieldSlt.replaceWith($(data).html());
	  	      	});
         }
       });
    }
	});
</script>